﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Win32;

namespace OpenDynDnsChanger.Service
{
    public static class ConfigHelper
    {
        public static void Init()
        {
            try
            {
                configKey = Registry.LocalMachine.OpenSubKey("Software\\OpenDynDnsChanger\\Config", true);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(Consts.ServiceName, ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error);
            }
        }

        private static RegistryKey configKey;

        public static double UpdateTime
        {
            get
            {
                object value = configKey.GetValue("UpdateTime");
                double dvalue = 0;
                if(double.TryParse(value.ToString(),out dvalue))
                    return dvalue;
                else
                    return 60 * 1000d;
            }
            set
            {
                configKey.SetValue("UpdateTime", value);
            }
        }

        public static string Username
        {
            get
            {
                object value = configKey.GetValue("Username");
                return value != null ? value.ToString() : "";
            }
            set
            {
                if (value != null)
                    configKey.SetValue("Username", value.ToString());
            }
        }

        public static string Passwort
        {
            get
            {
                object value = configKey.GetValue("Password");
                return value != null ? value.ToString() : "";
            }
        }

        public static string Provider
        {
            get
            {
                object value = configKey.GetValue("Provider");
                return value != null ? value.ToString() : "";
            }
        }

    }
}
